/**
* 新增-通告提示
*/
<template>
    <div class="layout-parent w100 p10">
        <ProjectTechBulletin ref="ProjectTechBulletinRef" :id="id" :type="type"></ProjectTechBulletin>

        <PageBottom>
            <vp-btn-save :loading="saveLoading" @click="handleSave(10)" />
            <VpProcessToolBar :loading="saveLoading" :set-loading-fn="(_loading: boolean) => { saveLoading = _loading }"
                :before-show-fn="beforeShowFn" :after-agree-fn="afterAgreeFn" />
        </PageBottom>

    </div>
</template>
<script setup lang="ts" name="addProjectTechBulletin">
import other from '/@/utils/other';
import { onMounted, ref, watch } from 'vue'
import request from '/@/utils/request';



const emits = defineEmits(['formSubmited']);
import VpProcessToolBar from '/@/components/vp/workflow/VpProcessToolBar.vue';
import ProjectTechBulletin from './ProjectTechBulletin.vue';
import PageBottom from '/@/components/vp/PageBottom/index.vue';
import mittBus from '/@/utils/mitt';
import { useRoute } from 'vue-router';
import { ElMessage } from "element-plus";


const loading = ref(false);
const saveLoading = ref(false);
const type = ref('add');


const props = defineProps<{
    id: string,
    type: string
}>();

//from表单
const ProjectTechBulletinRef = ref();
//流程
const processHandleRef = ref();

const route = useRoute();
// 保存信息
const handleSave = (zt: number) => {
    if (loading.value) {
        return;
    }
    saveLoading.value = true;
    ProjectTechBulletinRef.value.getData(zt).then((data: any) => {
        data = other.deepClone(data);
        console.log(data)
        const groupMap = new Map();
        data.detailList.forEach((item: any) => {
            item.detailUseList = []
            if (item.yt) {
                item.detailUseList = item.yt.map((sitem: any) => {
                    return { gx: sitem }
                })
            }
            const { num, dxjh, dxjm, lb, zt, fxsm, pgjl, pgdid, ...rest } = item; // 提取num，其余字段保留

            if (!groupMap.has(num)) {
                // 若该num未创建分组，初始化一个新分组
                groupMap.set(num, { num, dxjh, dxjm, lb, zt, fxsm, pgjl, pgdid, detailOemList: [] });
            }

            // 将当前对象添加到对应分组的items中
            groupMap.get(num).detailOemList.push(rest);
        });
        data.detailList = Array.from(groupMap.values()).sort((a, b) => a.num - b.num)

        var url = "/engTec/ccmEqvDoc/add";
        if (data.fBbid) {
            url = "/engTec/ccmEqvDoc/revision";
        }

        request.post(url, {
            ...data,
            zt: zt,
            processHandle: processHandleRef.value.getFormData(),
        }).then(() => {
            mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
            ElMessage.success(zt == 10 ? "操作成功/Success." : "办理完毕/Completed Processing");
        }).catch(() => {
        }).finally(() => {
            saveLoading.value = false;
        })
    }).catch(() => { saveLoading.value = false; });
}

const afterAgreeFn = (processHandle: any) => {
    saveLoading.value = true;
    ProjectTechBulletinRef.value.getData(20).then((data: any) => {
        request.post("/material/demand/" + type.value, {
            ...data,
            zt: 20,
            processHandle: processHandle,
        }).then(() => {
            ElMessage.success("操作成功/Operation Succeeded");
            emits("formSubmited");
        }).catch(() => {
        }).finally(() => { saveLoading.value = false; });
    }).catch(() => { saveLoading.value = false; });
}

const beforeShowFn = () => {
    saveLoading.value = true;
    return ProjectTechBulletinRef.value.getData(20).finally(() => { saveLoading.value = false; });
}


</script>
<style scoped lang="scss">
.card-header-center {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>